package org.paradise.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.paradise.dao.ShoppingDao;
import org.paradise.dao.ShoppingTypeDao;
import org.paradise.dao.ShoppingXsDao;
import org.paradise.pojo.Admin;
import org.paradise.pojo.Shopping;
import org.paradise.pojo.ShoppingType;
import org.paradise.pojo.ShoppingXs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Shopping")
public class ShoppingController {
    @Autowired
    private ShoppingDao shoppingDao;

    @Autowired
    private ShoppingTypeDao shoppingTypeDao;

    @Autowired
    private ShoppingXsDao shoppingXsDao;

    //查询所有商品数据调用的方法
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, Shopping condition) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Page<Shopping> pager = PageHelper.startPage(page,limit);
        List<Shopping> list = shoppingDao.getList(condition);
        map.put("count",pager.getTotal());  //条数
        map.put("data",list);   //数据
        map.put("code",0);   //状态
        map.put("msg","");   //返回的日志
        return map;
    }
    //添加商品
    @PostMapping("/addsp")
    @ResponseBody
    public Map<String,Object> addsp(Shopping shopping) throws Exception{
        Map<String,Object> map=new HashMap<>();
        shoppingDao.add(shopping);
        map.put("result",true);
        return map;
    }
    //批量删调用的方法
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelete(Integer[] id) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        shoppingDao.batchDelete(id);
        map.put("result",true);
        return map;
    }
    //查询商品类型
    @RequestMapping("/shoppingType")
    @ResponseBody
    public List<ShoppingType> search() throws Exception{
        return shoppingTypeDao.getAll();
    }
    //添加商品
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Shopping shopping)throws Exception{
        Map<String,Object> map = new HashMap<>();
        shoppingDao.add(shopping);
        map.put("result",true);
        return map;
    }
    //修改绑定值
    @RequestMapping("/idshopping")
    @ResponseBody
    public Shopping idshopping(Integer id)throws Exception{
        return shoppingDao.get(id);
    }
    //修改商品
    //修改用户调用的方法
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(Shopping shopping) throws Exception{
        Map<String,Object> map = new HashMap<>();
        shoppingDao.update(shopping);
        map.put("result",true);
        return map;
    }

    //添加商品销售单
    @RequestMapping(value = "/bathadd",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> bathadd(@Param("shoppingXs")ShoppingXs s ) throws Exception{
        Map<String,Object> map=new HashMap<>();
        shoppingXsDao.bathadd(s.getShoppingXs());
        map.put("result",true);
        return map;
    }

    //根据已选商品编号查询当前数量并且让商品库存减去当前获取到的数量
    @RequestMapping(value = "/updateTotal",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateTotal(@Param("id") Integer id,@Param("num") Integer num) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //根据编号查询商品信息
        Shopping shopping = shoppingDao.get(id);
        //获取商品库存
        shopping.getTotal();
        //修改商品库存
        shopping.setTotal(shopping.getTotal()-num);
        shoppingDao.upnum(shopping);
        map.put("result",true);
        return map;
    }
}